package com.mall.common.domain.manage;

import java.util.Iterator;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.PreRemove;
import javax.persistence.Table;

import com.mall.common.domain.BaseEntity;

/**
 * 实体类 - 角色
 */
@Entity
@Table(name="s_rbac_role")
public class Role extends BaseEntity {

	private static final long serialVersionUID = -6614052029623997372L;

	private String name;// 角色名称
	private String value;// 角色标识
	private Boolean isSystem;// 是否为系统内置角色
	private String description;// 描述
	private String operationBook;//操作手册
	
	private Set<Operate> operateSet;// 资源
	private Set<S_userDomain> userSet;
	private Set<Menu> menuSet;// 菜单

	@Column(nullable = false, unique = true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Column(nullable = false, unique = true)
	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	@Column(nullable = false, updatable = false,name="isSystem")
	public Boolean getIsSystem() {
		return isSystem;
	}

	public void setIsSystem(Boolean isSystem) {
		this.isSystem = isSystem;
	}

	@Column(length = 5000)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name="s_rbac_role_operate", joinColumns={@JoinColumn(name="role_id")},
			inverseJoinColumns={@JoinColumn(name="operate_id")})
	public Set<Operate> getOperateSet() {
		return operateSet;
	}

	public void setOperateSet(Set<Operate> operateSet) {
		this.operateSet = operateSet;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name="s_rbac_role_menu", joinColumns={@JoinColumn(name="role_id", nullable = false)},
			inverseJoinColumns={@JoinColumn(name="menu_id")})
	public Set<Menu> getMenuSet() {
		return menuSet;
	}

	public void setMenuSet(Set<Menu> menuSet) {
		this.menuSet = menuSet;
	}

	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "roleSet")
	public Set<S_userDomain> getUserSet() {
		return userSet;
	}

	public void setUserSet(Set<S_userDomain> userSet) {
		this.userSet = userSet;
	}

	public String getOperationBook() {
		return operationBook;
	}

	public void setOperationBook(String operationBook) {
		this.operationBook = operationBook;
	}

	@PreRemove
	public void preRemove(){
		Set<S_userDomain> userRoles = getUserSet();
		if (userRoles != null) {
			for (Iterator<S_userDomain> iterator = userRoles.iterator(); iterator.hasNext(); ){
				S_userDomain s_user = (S_userDomain)iterator.next();
				s_user.getRoleSet().remove(this);
			}
		}
		Set<Menu> menuRoles = getMenuSet();
		if (menuRoles != null) {
			for (Iterator<Menu> iterator = menuRoles.iterator(); iterator.hasNext();){
				Menu menu = (Menu)iterator.next();
				menu.getRoleSet().remove(this);
			}
		}
		Set<Operate> OperateRoles = getOperateSet();
		if (OperateRoles != null) {
			for (Iterator<Operate> iterator = OperateRoles.iterator(); iterator.hasNext();){
				Operate operate = (Operate)iterator.next();
				operate.getRoleSet().remove(null);
			}
		}
	}
}